package tech.wetech.admin3.sys.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.wetech.admin3.sys.model.AooAnotherMe;
import tech.wetech.admin3.sys.model.AooCompCertiData;
import tech.wetech.admin3.sys.model.AooPersCertiData;
import tech.wetech.admin3.sys.model.AooUser;
import tech.wetech.admin3.sys.repository.AooCompCertiDataRepository;
import tech.wetech.admin3.sys.repository.AooUserRepository;

import java.util.List;

@Service
public class AooCompCertiDataService {
  private AooCompCertiDataRepository compCertiDataRepository;
  private AooUserRepository aooUserRepository;
  private MsgCenterService msgCenterService;

  @Autowired
  public AooCompCertiDataService(
    AooCompCertiDataRepository aooCompCertiDataRepository,
    AooUserRepository aooUserRepository,
    MsgCenterService msgCenterService
  ) {
    this.compCertiDataRepository = aooCompCertiDataRepository;
    this.aooUserRepository = aooUserRepository;
    this.msgCenterService = msgCenterService;
  }

  @Transactional
  public AooCompCertiData save(AooCompCertiData aooCompCertiData) {
    return compCertiDataRepository.save(aooCompCertiData);
  }

  @Transactional
  public boolean resetStatus(Integer id, Integer newStatus, String checkDesc) {
    AooCompCertiData compCertiData = compCertiDataRepository.findById(id).orElse(null);
    try {
      if (compCertiData != null) {
        String email = compCertiData.getEmail();
        AooUser user = aooUserRepository.findByEmail(email);
        user.setStatus(newStatus);
        compCertiData.setStatus(newStatus);
        user.setCheckDesc(checkDesc);

        if (newStatus == 2) {
          user.setAuthType(2);
          // 审核通过的消息
          msgCenterService.createMessage("企业身份认证成功", "您提交的企业身份认证经审核通过，您可继续使用数字人身份创建和核验等功能。", email);
        } else if (newStatus == 3) {
          // 审核驳回的消息
          msgCenterService.createMessage(
            "企业身份认证失败", "您提交的企业身份认证经审核驳回，审核意见为" + checkDesc + "；您可确认相关内容后，点击“企业认证”再次提交身份认证。", email);
        }
        return true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  public List<AooCompCertiData> getAllData() {
    return compCertiDataRepository.findAll();
  }

  public List<AooCompCertiData> findByStatus(Integer status) {
    return compCertiDataRepository.findByStatus(status);
  }

  public List<AooCompCertiData> findByEmail(String email) {
    return compCertiDataRepository.findByEmail(email);
  }

  public List<AooCompCertiData> findByEmailAndStatus(String email, Integer status) {
    return compCertiDataRepository.findByEmailAndStatus(email, status);
  }

  public List<AooCompCertiData> findByCompanyNameContaining(String companyName) {
    return compCertiDataRepository.findByCompanyNameContaining(companyName);
  }

  public void deleteByEmail(String email) {
    compCertiDataRepository.deleteByEmail(email);
  }
}
